<?php

class AdminModule extends CWebModule
{
	public $defaultController = 'site';

	public function init()
	{
		// this method is called when the module is being created
		// you may place code here to customize the module or the application

		// import the module-level models and components
		$this->setImport(array(
			'admin.models.*',
			'admin.components.*',
		));

		$this->setComponents(array(
            'user' => array(
                'class' => 'WebUser',
                // 'loginUrl' => Yii::app()->createUrl('admin/site/login'),
            ),
        ));

        Yii::app()->user->setStateKeyPrefix('_admin');
	}

	public function beforeControllerAction($controller, $action)
	{
		if(parent::beforeControllerAction($controller, $action))
        {
            Yii::app()->errorHandler->errorAction='admin/site/error';
            $route = $controller->id . '/' . $action->id;
            $publicPages = array(
                'site/login',
                'site/error',
            );
            if (!in_array($route, $publicPages))
                if(!isset (Yii::app()->user->id))
                    Yii::app()->user->loginRequired();    
            return true;
        }
        else
			return false;  
	}
}
